
package com.wjt.homestayms.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wjt.homestayms.mapper.EmpMapper;
import com.wjt.homestayms.pojo.Employee;
import com.wjt.homestayms.common.PageRequest;
import com.wjt.homestayms.common.PageResult;
import com.wjt.homestayms.service.IEmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

@Service
public class EmpServiceImpl implements IEmpService {
    @Autowired
    EmpMapper empMapper;

    @Override
    public boolean addEmp(Employee employee) {
        return empMapper.addEmp(employee);
    }

    @Override
    public boolean updateEmp(Employee employee) {
        List<Employee> emp = empMapper.getEmpByEid(employee.geteId());
        if(emp.size() > 0){
            boolean flag = empMapper.updateEmp(employee);
            return flag;
        }
        return false;
    }

    @Override
    public Employee getEmp(Integer eId) {
        List<Employee> empList = empMapper.getEmpByEid(eId);
        Employee emp = null;
        Iterator<Employee> iterator = empList.iterator();
        if(iterator.hasNext()){
             emp = iterator.next();
        }
        return emp;
    }

    @Override
    public boolean deleteEmp(Integer eId) {
        List<Employee> emp = empMapper.getEmpByEid(eId);
        if(emp.size() > 0){
            boolean flag = empMapper.deleteEmp(eId);
            return flag;
        }
        return false;
    }

    @Override
    public PageResult getEmpList(PageRequest pageRequest, Integer id) {
        PageHelper.startPage(pageRequest.getPageNum(),pageRequest.getPageSize());
        List<Employee> empList = empMapper.getEmpList(id);

        return PageResult.toPageResult(empList);
    }

    @Override
    public PageResult getEmp(PageRequest pageRequest, Integer eId) {
        PageHelper.startPage(pageRequest.getPageNum(),pageRequest.getPageSize());
        List<Employee> empList = empMapper.getEmpByEid(eId);
        return PageResult.toPageResult(empList);
    }

    @Override
    public PageResult getEmp(PageRequest pageRequest, String empName) {
        PageHelper.startPage(pageRequest.getPageNum(),pageRequest.getPageSize());
        List<Employee> empList = empMapper.getEmpByName(empName);
        return PageResult.toPageResult(empList);
    }

    @Override
    public List<Employee> getEmps(Integer id) {
        return empMapper.getEmpList(id);
    }
}
